package com.wzq.animal.back.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.animal.back.pojo.Files;
import com.wzq.animal.back.pojo.User;
import com.wzq.animal.back.service.FilesService;
import com.wzq.animal.back.service.UserService;
import com.wzq.animal.common.Result;
import com.wzq.animal.fore.pojo.AnimalInfo;
import com.wzq.animal.fore.service.AnimalInfoService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/eCharts")
public class EChartsController {
    @Autowired
    private UserService userService;

    @Autowired
    private FilesService filesService;

    @Autowired
    private AnimalInfoService animalInfoService;

    /**
     * 获取用户性别数量
     * @return
     */
    @GetMapping("/getGenderProportion")
    public Result getGenderProportion(){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getGender,"男");
        int male = userService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.eq(User::getGender,"女");
        int female = userService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.isNull(User::getGender);
        int unknown = userService.list(queryWrapper).size();
        ArrayList<Integer> list = CollUtil.newArrayList(male, female, unknown);
        return Result.success(list);
    }

    /**
     * 文件大小统计
     * @return
     */
    @GetMapping("/getFilesSize")
    public Result getFilesSize(){
        LambdaQueryWrapper<Files> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(Files::getSize,100);
        int lessEqual100 = filesService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.gt(Files::getSize,100)
                .le(Files::getSize,200);
        int more100less200 = filesService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.gt(Files::getSize,200)
                .le(Files::getSize,500);
        int more200less500 = filesService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.gt(Files::getSize,500)
                .le(Files::getSize,1024);
        int more500less1024 = filesService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.gt(Files::getSize,1024)
                .le(Files::getSize,10240);
        int more1024less10240 = filesService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.gt(Files::getSize,10240);
        int more10240 = filesService.list(queryWrapper).size();
        ArrayList<Integer> list = CollUtil.newArrayList(lessEqual100, more100less200, more200less500, more500less1024, more1024less10240, more10240);
        return Result.success(list);
    }

    /**
     * 对各种信息总量的统计
     * @return
     */
    @GetMapping("/getMoreCount")
    public Result getMoreCount(){
        MoreCount moreCount = new MoreCount();
        moreCount.setFiles(filesService.count());
        moreCount.setUsers(userService.count());
        moreCount.setAnimals(animalInfoService.count());
        return Result.success(moreCount);
    }

    /**
     * 不同保护等级动物数量统计
     * @return
     */
    @GetMapping("/getAnimalLevelCount")
    public Result getAnimalLevelCount(){
        LambdaQueryWrapper<AnimalInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnimalInfo::getProtectionLevel,"国家一级");
        Integer country1 = animalInfoService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getProtectionLevel,"国家二级");
        Integer country2 = animalInfoService.list(queryWrapper).size();
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getProtectionLevel,"省重点");
        Integer province = animalInfoService.list(queryWrapper).size();
        ArrayList<Integer> list = CollUtil.newArrayList(country1, country2, province);
        return Result.success(list);
    }

    @GetMapping("/getClassCount")
    public Result getClassCount(){
        ClassCount classCount = new ClassCount();
        LambdaQueryWrapper<AnimalInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnimalInfo::getClassName,"哺乳纲");
        classCount.setMammalia(animalInfoService.count(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getClassName,"鸟纲");
        classCount.setAves(animalInfoService.count(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getClassName,"爬行纲");
        classCount.setReptilia(animalInfoService.count(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getClassName,"两栖纲");
        classCount.setAmphibia(animalInfoService.count(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getClassName,"鱼纲");
        classCount.setPisces(animalInfoService.count(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq(AnimalInfo::getClassName,"昆虫纲");
        classCount.setInsecta(animalInfoService.count(queryWrapper));
        queryWrapper.clear();
        return Result.success(classCount);
    }

    @Data
    class MoreCount{
        int files;
        int users;
        int animals;
    }

    @Data
    class ClassCount{
        int mammalia;//哺乳纲
        int aves;//鸟纲
        int reptilia;//爬行纲
        int amphibia;//两栖纲
        int pisces;//鱼纲
        int insecta;//昆虫纲
    }
}
